<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>管理</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="./js/jquery-1.11.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/main.js"></script>
    <style type="text/css">
        body {
            background: #2fb9e2;
        }
        
        .content {
            margin: auto;
            margin-top: 100px;
            padding: 5%;
            padding-top: 2%;
            width: auto;
            min-width: 1000px;
            height: auto;
            background-color: #fff;
            border-radius: 20px;
            box-shadow: 0px 0px 20px #5b7884;
        }
        
        .table {
            text-align: center;
            margin: auto;
            padding-bottom: 10px;
        }
        .btn {
            margin-left: 10px;
        }
        #add {
            float: right;
        }
        
        #order {
            float: right;
            margin-right: 15px;
        }
        
        #title {
            margin: auto;
            padding-top: 5px;
            padding-bottom: 5px;
        }
        
        .center {
            text-align: center;
        }
        .modal-body {
            padding-left: 50px;
            padding-right: 50px;
        }
    </style>

</head>
    <script>
        if (!getCookie('sessionid')){
            alert("没有登录")
            window.location.href = "login.html"
        }
    var current_page = 1;
   
    $(document).ready(function() {
        getList(1)

        $("#confirm_delete").click(function () {
            alert("delete");
        })

        $("#order").click(function(){
            //alert("order");
            direct_order();
        })
        $("#modal_delete").on('show.bs.modal',function(e){
            //alert('show');
            //var s = 
            //$("#delete_name").text("1");
        });   
        
        $("#confirm_add").click(function(){
            //alert("add");
            var username = $("#username").val();
            var name = $("#dama_name").val();
            //alert("username:" + username + ",name:" + name);
            if (!checkInput(username) || !checkInput(name)) {
                return
            }

            addDama(username,name);
        })

        $("#prev").click(function () {
            if (current_page==1) {
                alert("已经是第一页")
            }else{
                prev_page()
            }
        })

        $("#next").click(function () {
            next_page()
        })
    })
    function getStatusString(status){
         var str = new Array("休息中","接受预订","开店中");
        return str[status];
    }
    function addRow(id,name,status,price,rating){
        //if (address == undefined){
            //address = "空"
        //}
        //address = ;
        var contentTr = '<tr id="tr_' + id + '">';
        contentTr += '<td>'+name+'</td>';
        contentTr += '<td>'+getStatusString(status)+'</td>';
        contentTr += '<td>'+price+'</td>';
        contentTr += '<td>'+rating+'</td>';
        contentTr += '<td>';
        contentTr += '<button class="btn btn-sm btn-default" onclick="direct_detail('+id+')">详情</button>';
        contentTr += '<button class="btn btn-sm btn-default" onclick="direct_modify('+id+')">修改</button>';
        //contentTr += '<button class="btn btn-sm btn-danger" data-toggle="modal" id="dama_delete' + id + '" onclick="delete_show(' + id +',\''+name+'\')">删除</button>';
        contentTr += '</td>';
        contentTr + '</tr>';
        return contentTr;
    }
     function checkInput(string) {
        var jgpattern = /^[A-Za-z0-9]+$/;
        if (string.length == 0) {
            alert("空")
            return false;
        } else if (string.match(" ")) {
            alert("不能包含空格")
            return false;
        }
        return true;
    }

    function addDama(username,name){
        var session = getCookie('sessionid')
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: {
                    module: "admin",
                    action: "addProvider",
                    username: username,
                    name: name,
                    sessionid: session
                    
                },
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            $("#modal_add").modal('hide')
                            getList(1)
                            current_page = 1
                            alert("添加成功")
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        }else if (data.errorCode == 1004) {
                            alert("没有权限")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1030) {
                            alert("用户名已存在")
                        }
                        //alert("data:" + data.data.aid + ",status:" + textStatus)
                    }

                },
            };
            $.ajax(settings);
    }
    function getList(page){
        //访问网络
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: {
                    module: "provider",
                    action: "getProviderList",
                    page: page
                },
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            //alert("data:" + data.data);
                            var colTr = '<tr><td colspan="1">姓名</td><td>状态</td><td>起送价格</td><td>评分</td><td colspan="2">操作</td></tr> ';
                            var contentTr;
                            $.each(data.data,function(index,value){
                                contentTr += addRow(value.pid,value.name, value.status,value.delivermin,value.rating.rating);
                                
                            })
                            
                            var tableTr = colTr + contentTr;
                            $("#content_table").html(tableTr);
                            
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")
                        }
                        //alert("data:" + data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                },
            };
            $.ajax(settings);
    }

    function deletDama(){
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: {
                    module: "admin",
                    action: "addProvider",
                    username: username,
                    password: name
                },
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        }else if (data.errorCode == 1004) {
                            alert("没有权限")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1030) {
                            alert("用户名已存在")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")
                        }
                        
                        //alert("data:" + data.data.aid + ",status:" + textStatus)
                    }

                },
            };
            $.ajax(settings);
    }
    
    function delete_show (id,name){
        //$("#delete_id").val(id);
            //alert($("#delete_id").val())
        //$("#delete_name").text(name);
        //$("#modal_delete").modal('show');
    }

    function direct_detail(id){
        window.location.href = "detail.html?id="+id
    }

    function direct_order(){
        window.location.href = "order.html"
    }

    function direct_modify(id){
        window.location.href = "modify.html?id="+id
    }

   
    function next_page () {
        current_page++;
        getList(current_page)
    }

    function prev_page () {
        current_page--;
        getList(current_page);
    }
        


</script>

<body>
    <div class="modal fade" id="modal_add">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <!-- <span aria-hidden="true">&times;</span>
                    -->
                </button>
                <h4 class="modal-title">饭享乐-添加大妈信息</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" name="module" value="admin">
                    <input type="hidden" name="action" value="login">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" placeholder="用户名"></div>
                    <div class="form-group">
                        <label for="dama_name">姓名</label>
                        <input type="text" class="form-control" id="dama_name" name="name" placeholder="姓名"></div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="confirm_add">提交</button>
            </div>
        </div>
        <!-- /.modal-content --> </div>
    <!-- /.modal-dialog -->
</div>

<div class="modal fade" id="modal_delete">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <!-- <span aria-hidden="true">&times;</span>
                -->
            </button>
            <h4 class="modal-title">饭享乐-删除大妈</h4>
        </div>
        <div class="modal-body">
            <p>
                是否需要删除大妈：
                <span id="delete_name"></span>
            </p>
            <input type="hidden" value="" id="delete_id"></div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-danger" id="confirm_delete">删除</button>
        </div>
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<div class="container">
<div class="content">
    <h1 class="text-center">饭享乐</h1>
    <div class="panel panel-info">

        <div class="panel-heading">
            <button class="btn btn-sm btn-primary" id="add" data-toggle="modal" data-target="#modal_add">添加</button>
            <button class="btn btn-sm btn-primary" id="order">切换订单列表</button>
            <p id="title"> <strong>大妈列表</strong>
            </p>
        </div>

        <div class="list-group">

            <table class="table table-striped" id="content_table">
               
            </table>
        </div>

    </div>

    <nav class="center">
        <ul class="pager">
            <li>
                <a aria-label="Previous" id="prev">
                    <span aria-hidden="true">上一页</span>
                </a>
            </li>
            <li>
                <a aria-label="Next" id="next">
                    <span aria-hidden="true">下一页</span>
                </a>
            </li>
        </ul>
    </nav>
</div>
</div>
</body>

</html>